import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/app_constant.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/utils.dart';
import 'package:tender_flutter/widgets/text_highlight.dart';

import '../model/tender_entity.dart';

typedef Future<bool> CollectionCallback();

class TenderListCell extends StatefulWidget {
  const TenderListCell(
      {Key key, this.data, this.onCollection, this.onTap, this.highlightWords})
      : super(key: key);

  final TenderList data;
  final List<String> highlightWords;
  final CollectionCallback onCollection;
  final GestureTapCallback onTap;

  @override
  _TenderListCellState createState() => _TenderListCellState();
}

class _TenderListCellState extends State<TenderListCell> {
  @override
  Widget build(BuildContext context) {
    return _initCell();
  }

  Widget _initCell() {
    return GestureDetector(
      onTap: widget.onTap,
      child: Container(
        color: ThemeColors.colorWhite,
        padding: EdgeInsets.all(ScreenAdapter.width(30)),
        child: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                      flex: 6,
                      child: TextHighlight(
                        content: widget.data.title ?? "-",
                        // keyWords: widget.highlightWords,
                        keyWords: widget.data.words,
                        maxLines: 2,
                        normalStyle: TextStyle(
                            color: ThemeColors.colorType1,
                            fontSize: ScreenAdapter.sp(29),
                            fontWeight: FontWeight.w500),
                        highlightStyle: TextStyle(
                            color: ThemeColors.colorType56,
                            fontSize: ScreenAdapter.sp(29),
                            fontWeight: FontWeight.w500),
                      )),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                        onTap: () async {
                          widget.onCollection().then((value) => {
                                /// 请求成功改变状态
                                if (value)
                                  {
                                    setState(() {
                                      widget.data.isFocus =
                                          !widget.data.isFocus;
                                    })
                                  }
                              });
                        },
                        child: Image.asset(
                          RPath.imgPath +
                              (widget.data.isFocus ?? false
                                  ? "icon_favorites_select.png"
                                  : "icon_favorites_normal.png"),
                          width: ScreenAdapter.width(35),
                          height: ScreenAdapter.width(35),
                        )),
                  )
                ],
              ),
              SizedBox(height: ScreenAdapter.height(15)),
              Offstage(
                  offstage: (widget.highlightWords != null &&
                          widget.highlightWords.isNotEmpty &&
                          !isEmpty(widget.data.untagContent))
                      ? false
                      : true,
                  child: Container(
                      padding:
                          EdgeInsets.only(bottom: ScreenAdapter.height(15)),
                      child: TextHighlight(
                        maxLines: 1,
                        content: widget.data.untagContent ?? "-",
                        keyWords: widget.highlightWords,
                        normalStyle: TextStyle(
                            color: ThemeColors.colorType2,
                            fontSize: ScreenAdapter.sp(26),
                            fontWeight: FontWeight.w500),
                        highlightStyle: TextStyle(
                            color: ThemeColors.colorType56,
                            fontSize: ScreenAdapter.sp(26),
                            fontWeight: FontWeight.w500),
                      ))),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      _getTagOne(),
                      _getTagTwo(),
                      _getTagThird()
                    ],
                  ),
                  // IconText(" " + widget.data.date ?? "",
                  //     icon: Image.asset(RPath.imgPath + "icon_date.png"),
                  //     iconSize: 12,
                  //     style: TextStyle(
                  //         color: ThemeColors.colorType1,
                  //         fontSize: ScreenAdapter.sp(24)))
                  Row(children: [
                    Image.asset(RPath.imgPath + "icon_date.png",
                        width: ScreenAdapter.width(30),
                        height: ScreenAdapter.width(30)),
                    Text(" " + widget.data.date ?? "",
                        style: TextStyle(
                            color: ThemeColors.colorType1,
                            fontSize: ScreenAdapter.sp(24)))
                  ]),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  /// 获取标签
  Widget _getTagOne() {
    String title;
    if (widget.data.typeCode == int.parse(AppConstant.TENDERTYPE_RAILWAY) ||
        widget.data.typeCode == int.parse(AppConstant.TENDERTYPE_RAILWAYWINNING)) {
      // 铁路信息
      title = widget.data.companyName;
    } else {
      title = widget.data.address;
    }
    if (!isEmpty(title)) {
      return Container(
        constraints: BoxConstraints(maxWidth: ScreenAdapter.width(200)),
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdapter.width(20),
            vertical: ScreenAdapter.width(10)),
        child: Text(title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                color: ThemeColors.colorType40,
                fontSize: ScreenAdapter.sp(24))),
        decoration: BoxDecoration(
            color: ThemeColors.colorType20,
            borderRadius: new BorderRadius.circular(3)),
      );
    }
    return Container();
  }

  Widget _getTagTwo() {
    if (widget.data.industry != null && widget.data.industry.isNotEmpty) {
      return Container(
        margin: EdgeInsets.only(left: ScreenAdapter.width(15)),
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdapter.width(20),
            vertical: ScreenAdapter.width(10)),
        decoration: BoxDecoration(
            color: ThemeColors.colorType15,
            borderRadius: new BorderRadius.circular(3)),
        child: Text(widget.data.industry ?? "",
            style: TextStyle(
                color: ThemeColors.colorType16,
                fontSize: ScreenAdapter.sp(24))),
      );
    }
    return Container();
  }

  Widget _getTagThird() {
    //typeCode 为TENDERTYPE_WINBIDBING标识中标公告
    if (widget.data.typeCode != int.parse(AppConstant.TENDERTYPE_WINBIDBING) &&
        widget.data.price != null &&
        widget.data.price.isNotEmpty &&
        widget.data.price != "0") {
      String money = "";
      int price = int.parse(widget.data.price);
      if (price < 10000) {
        money = (price / 10000).toString();
      } else {
        money = (price ~/ 10000).toString();
      }

      return Container(
        margin: EdgeInsets.only(left: ScreenAdapter.width(15)),
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdapter.width(20),
            vertical: ScreenAdapter.width(10)),
        decoration: BoxDecoration(
            color: ThemeColors.colorType41,
            borderRadius: new BorderRadius.circular(3)),
        child: Text(money + "万" ?? "",
            style: TextStyle(
                color: ThemeColors.colorType42,
                fontSize: ScreenAdapter.sp(24))),
      );
    }
    return Container();
  }
}
